package org.ar.arkantos.persistence;

import java.util.List;

import org.apache.log4j.Logger;
import org.ar.arkantos.model.Registro;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class RegistroHibernateDao implements RegistroDao {
	
	private Registro registro;
	private List<Registro> registros;
	
	Logger logger = Logger.getLogger("---log---");
	
	public RegistroHibernateDao() {
	}
	
	@SuppressWarnings("unchecked")
	public List<Registro> getAllRegistros() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			String hql = "from Registro";
			Query query = session.createQuery(hql);
			registros = query.list();
			return registros;
		} catch (HibernateException ex) {
			throw ex;
		} finally {
			session.close();
			System.out.println("-----------------entro a allregistros");
		}
	}
	
	public Registro getRegistro (Integer id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		
		try {
			session.beginTransaction();
			Query q = session.createQuery("from Registro as reg where reg.id =:registroId");
			q.setInteger("registroId", id);
			return (Registro) q.uniqueResult();
		} catch (HibernateException ex) {
			throw ex;
		} finally {
			session.close();
			System.out.println("-----------------entro a getregistro");
		}
	}
	
	public void insertRegistro(Registro reg) {
		Session session = null;
		Transaction tx = null;
		
		try {
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			logger.info("session2 is connected: " + session.isConnected());
			logger.info("tx: " + tx);
			logger.info("session2 is open: " + session.isOpen());
			session.persist(reg);
			logger.info("session3 is open: " + session.isOpen());
			tx.commit();
		} catch (RuntimeException e) {
			if(tx != null)
				tx.rollback();
			throw e;
		} finally {
			//session.close();
			System.out.println("-----------------entro a insert");
		}
	}
	
	public void updateRegistro(Registro reg) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = null;
		
		try {
			tx = session.beginTransaction();
			session.update(reg);
			tx.commit();
		} catch (RuntimeException e) {
			if(tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
			System.out.println("-----------------entro a update");
		}
	}
	
	public void deleteRegistro(Integer id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = null;
		
		try {
			tx = session.beginTransaction();
			registro = (Registro)session.get(Registro.class, id);
			session.delete(registro);
			tx.commit();
		} catch (RuntimeException e) {
			if(tx != null)
				tx.rollback();
			throw e;
		} finally {
			System.out.println("-----------------entro a delete");
		}
	}

}
